<script setup lang="ts">
import { delRoleUserAPI, getJueSeListAPI, getRoleDetailAPI, getRoleUserAPI, getTreeAPI, updateRoleAPI } from '@/apis/system';
import type { Role, RoleData, RoleUser } from '@/types/system';
import { ref } from 'vue';
import userIcon from '@/assets/user.svg';
import userIconActive from '@/assets/user-active.svg'
import { ElMessage, ElMessageBox, type TreeInstance } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';
import router from '@/router';

const RoleList = ref<Role[]>([]);

const getRoleList = async () => {
  const res = await getJueSeListAPI();
  RoleList.value = res.data;
  // 获取第一个角色详情
  const rowId = res.data[0].roleId
  await getRoleDetail(rowId)

  // 获取第一个角色成员
  await getRoleUser(rowId)
}
getRoleList()

// 2. 点击激活交互
const activeIndex = ref(0)
const changeRole = async (index: number, roleId: number) => {
  // 2.1 设置高亮效果
  activeIndex.value = index
  // 2.2 获取角色详情
  getRoleDetail(roleId)
}

// 获取角色详情
const perms = ref<number[][]>([])
const treeRefs = ref<TreeInstance[]>([])
const userTotal = ref(0)
const getRoleDetail = async (roleId:number) => {
  const res = await getRoleDetailAPI(roleId)
  // console.log('res', res)
  perms.value = res.data.perms
  userTotal.value = res.data.userTotal

  // 切换时,设置选中的权限
  treeRefs.value.forEach((tree, index) => {
    tree.setCheckedKeys(perms.value[index])
  })
}

const treeList = ref<RoleData[]>([])
const getTreeList = async () => {
  const res = await getTreeAPI();
  treeList.value = res.data;
}
getTreeList();

// 5. tab切换
const activeTabName = ref<'tree' | 'member'>('tree')

// 获取角色成员
const roleUserList = ref<RoleUser[]>([])
const getRoleUser = async (roleId:number) => {
  const res = await getRoleUserAPI(roleId)
  console.log('res', res.data)
  roleUserList.value = res.data
}



// 删除角色
const onclick = async (roleId:number) => {
  ElMessageBox.confirm('此操作将永久删除该角色, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async() => {
    await delRoleUserAPI(roleId)
    ElMessage.success('删除成功')
    getRoleList()
  }).catch(() => {
  });
}

</script>

<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div 
        class="role-item" 
        v-for="(item,index) in RoleList" 
        :key="item.roleId"
        @click="changeRole(index, item.roleId)"
        :class="{ active: index === activeIndex}"
      >
        <div class="role-info">
          <img :src="index === activeIndex ? userIconActive : userIcon" class="icon" />
        </div>
        <div>
          <span>{{ item.roleName }}</span>
        </div>
        <div class="more">
          <!-- <img src="@/assets/more.svg" class="icon" /> -->
            <el-dropdown>
              <span class="el-dropdown-link">
                <img src="@/assets/more.svg" class="icon" />
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="$router.push(`/addRole/${item.roleId}`)"> 
                    <el-button link>编辑角色</el-button>
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <el-button type="danger" link @click="onclick(item.roleId)">删除</el-button>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="small" @click="$router.push('/addRole')">添加角色</el-button>
    </div>
    <!-- 右侧权限和成员 -->
     <el-tabs v-model="activeTabName">
          <el-tab-pane label="功能权限" name="tree">
            <div class="right-wrapper">
            <div class="tree-wrapper">
            <div v-for="(item,index) in treeList" 
            :key="item.id" 
            class="tree-item"
            >
              <div class="tree-title">{{ item.title }}</div>
              <el-tree 
              :data="item.children" 
              :props="{ label:'title',disabled:() =>true}"
              default-expand-all
              show-checkbox
              :default-checked-keys="perms[index]"
              node-key="id"
              ref="treeRefs"
              ></el-tree>
            </div>
          </div>
        </div> 
      </el-tab-pane>
      <el-tab-pane :label="`成员(${userTotal})`" name="member">
         <div class="user-wrapper">
            <el-table :data="roleUserList">
              <el-table-column type="index" width="250" label="序号" />
              <el-table-column prop="name" label="员工姓名" />
              <el-table-column prop="userName" label="登录账号" />
            </el-table>
          </div>
      </el-tab-pane>
     </el-tabs>
  </div>
</template>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;

  .left-wrapper {
    width: 220px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
        width: 20px;
      }
    }

    .more {
      display: flex;
      align-items: center;
      .icon {
          width: 15px;
        }
    }

    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;

        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
.el-tabs{
  width: 90%;
  margin-left: 10px;
}
.icon{
  border: none;
}
</style>